// components/carousel/carousel.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carousels: {
      type: Array,
      value: [],
      observer: function(newVal, oldVal, changePath) {
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    scrollDist: '', //向左滑动的距离
    currentIndex: 0, //目前图片的index
    screenWidth: 0, //屏幕宽度
    timer: null, //定时器
    autoPlay:true,//是否是自动轮播
    touchPoint:0,//触摸点
    touchStartTime:'',//触摸开始时间
    outTimer:null,//延时器
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @method
     * @desc 启动定时器
     */
    startTimer: function() {
      let that=this
      if(that.data.timer){
        that.stopTimer()
      }
      this.setData({
        timer: setInterval(function () {
          if (that.data.currentIndex <= (that.properties.carousels.length - 2)) {
            that.setData({
              currentIndex: that.data.currentIndex + 1,
            })
            that.setData({
              scrollDist: that.data.screenWidth * 0.8 * that.data.currentIndex
            })
          } else {
            that.setData({
              currentIndex: 0,
              scrollDist: 0,
              autoPlay:true
            })
          }
        }, 4000)
      })
    },
    /**
     * @method
     * @desc 停止定时器
     */
    stopTimer:function(){
      clearInterval(this.data.timer)
      this.setData({
        timer:null,
        autoPlay:false
      })
    },
    /**
     * @method
     * @desc 监听滑动
     */
    scrolling(e){
      let that=this
      let temScrollDist = e.detail.scrollLeft
      if (!that.data.autoPlay){
        for (let i = 0; i < (that.properties.carousels.length); i++) {
          if (temScrollDist >= (that.properties.carousels.length - 1 - i) * that.data.screenWidth * 0.8) {
            that.setData({
              currentIndex: that.properties.carousels.length - i - 1
            })
            return
          }
        }
      }
    },
    /**
     * @method
     * @desc 跳转轮播图详情
     */
    goToDetail(e){
      let that=this
      let detail={
        id: e.currentTarget.dataset.item.fId
      }
      let option={}
      that.triggerEvent('goToDetail',detail,option)
    },
    //加入购物车
    addCart(e) {
      let that = this
      if (e.currentTarget.dataset.item.fTotalCount>0){
        let detail = {
          id: e.currentTarget.dataset.id,
          count: e.currentTarget.dataset.count
        }
        let option = {}
        that.triggerEvent('openCartModal', detail, option)
      }else{
        wx.showToast({
          title: '库存不足',
          icon:'none'
        })
      }
      
    },
    /**
     * @method
     * @desc 触摸开始
     */
    touchStart(e){
      let that=this
      that.stopTimer()
      that.setData({
        autoPlay:false
      })
      that.setData({
        touchStartTime: new Date().getTime(),
        touchPoint: e.touches[0].pageX
      })
    },
    /**
     * @method
     * @desc 触摸结束
     */
    touchEnd(e){
      let that=this
      
      let touchEndPoint = e.changedTouches[0].pageX
      let touchEndTime=new Date().getTime()
      //向左滑动
      if ((touchEndTime - that.data.touchStartTime < 1000) && (touchEndPoint - that.data.touchPoint<0)){
        if (that.data.outTimer){
          clearTimeout(that.data.outTimer)
        }
        that.setData({
          outTimer: setTimeout(function () {
            that.startTimer()
            that.setData({
              autoPlay: true
            })
          }, 1500)
        })
      }else if ((touchEndTime - that.data.touchStartTime < 1000) && (touchEndPoint - that.data.touchPoint > 0)) {
        if (that.data.outTimer) {
          clearTimeout(that.data.outTimer)
        }
        that.setData({
          outTimer: setTimeout(function () {
            that.startTimer()
            that.setData({
              autoPlay: true
            })
          }, 1500)
        })
      }else{
        that.startTimer()
        that.setData({
          autoPlay: true
        })
      }
    }
  },

  ready() {
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollDist: res.screenWidth * 0.8 * that.data.currentIndex,
          screenWidth: res.screenWidth
        })
      }
    })
    that.startTimer()
  }

})